package com.ildepinc.persistence.concretes;

import java.io.File;
import java.util.Properties;

import com.ildepinc.persistence.abstracts.JdbcDriverBase;

public class JdbcDriver extends JdbcDriverBase {

  private Properties properties;

  public JdbcDriver() {
    this.clear();
  }

  public JdbcDriver(String propertiesPath, String propertiesEnv, String propertiesName, String dbPath)
      throws Exception {
    PropertiesReader propertiesLoader = new PropertiesReader(propertiesPath, propertiesEnv, propertiesName);
    this.properties = propertiesLoader.getProperties();
    String driverPort = null;
    String driverEmbedded = null;
    String databaseCreateIfNone = null;
    this.setDriverClass(this.properties.getProperty("driver.driverClass"));
    this.setDatabaseEngine(this.properties.getProperty("driver.databaseEngine"));
    this.setUrl(this.properties.getProperty("driver.url"));
    this.setHost(this.properties.getProperty("driver.host"));
    driverPort = this.properties.getProperty("driver.port");
    if (driverPort.length() > 0) {
      try {
        this.setPort(Integer.valueOf(driverPort));
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    if (dbPath.length() > 0) {
      this.setDatabase(
          dbPath + File.separator + propertiesEnv + File.separator + this.properties.getProperty("driver.database"));
    } else {
      this.setDatabase(propertiesEnv + File.separator + this.properties.getProperty("driver.database"));
    }
    this.setUser(this.properties.getProperty("driver.user"));
    this.setPassword(this.properties.getProperty("driver.password"));
    driverEmbedded = this.properties.getProperty("driver.embedded");
    if (driverEmbedded.length() > 0) {
      try {
        this.setEmbedded(Boolean.valueOf(driverEmbedded));
      } catch (Exception e) {
        e.printStackTrace();
      }

    }
    databaseCreateIfNone = this.properties.getProperty("database.createifnone");
    if (databaseCreateIfNone.length() > 0) {
      try {
        this.setDatabaseCreateIfNone(Boolean.valueOf(databaseCreateIfNone));
      } catch (Exception e) {
        e.printStackTrace();
      }

    }
  }

  @Override
  // You should customize this method
  // depending on the database engine
  // you want to use.
  // This version is for Apache Derby
  public String composeFullUrl() {
    String fullUrl = this.getUrl();
    File dbFile = null;
    fullUrl += ":";
    fullUrl += this.getDatabaseEngine();
    fullUrl += ":";
    if (!this.isEmbedded()) {
      fullUrl += "//";
      fullUrl += this.getHost();
      fullUrl += ":";
      fullUrl += this.getPort().toString();
      fullUrl += File.separator;
    }
    if (this.isEmbedded()) {
      dbFile = new File(this.getDatabase());
      if (dbFile.exists() && dbFile.isDirectory()) {
        fullUrl += this.getDatabase();
      } else {
        if (this.isDatabaseCreateIfNone()) {
          fullUrl += this.getDatabase() + ";create=true";
        }
      }
    }
    return fullUrl;
  }

  public Properties getProperties() {
    return properties;
  }

  public void setProperties(Properties properties) {
    this.properties = properties;
  }

  @Override
  protected void clear() {
    super.clear();
    this.properties = null;
  }

}
